//
//  VideoPlayerBaseLayer.swift
//  VideoPlayer
//
//  Created by JY_NEW on 2021/10/13.
//

import Foundation

/// 浮层基类，只实现基本及常见的操作，由使用者继承后画界面
open class VideoPlayerBaseLayer: NSObject, VideoPlayerLayerType {
    
    open var identifier: Int?
    
    open weak var playerManager: VideoPlayerManagerType? {
        didSet {
            if oldValue !== self.playerManager {
                if let old = oldValue {
                    self.unloadPlayerManager(old)
                }
                if let new = self.playerManager {
                    self.loadPlayerManager(new)
                }
            }
        }
    }
    
    open weak var manager: VideoPlayerLayerManagerType? {
        didSet {
            if oldValue !== self.manager {
                if let old = oldValue {
                    self.unloadManager(old)
                }
                if let new = self.manager {
                    self.loadManager(new)
                }
            }
        }
    }
    
    open var status: VideoPlayerLayerActivationStatus = .deactived
    open var isDisabled: Bool = false {
        willSet {
            ///激活中被禁用的话要同时关掉
            if newValue && self.status.isActived {
                self.deactive()
            }
        }
    }
    open var category: VideoPlayerLayerCategory {
        return .control
    }
    
    /// 临时等级，用过一次就清除
    private var tempLevel: VideoPlayerLayerLevel?
    
    open var level: VideoPlayerLayerLevel {
        if let temp = self.tempLevel {
            return temp
        }
        switch self.category {
        case .plugin: return .plugin
        case .state: return .state
        case .control: return .control
        case .block: return .block
        case .other: return .normal
        }
    }
    public lazy var autoPauseFlag: Int? = {
        return self.autoPause ? makePauseFlag() : nil
    }()
    /// 默认Block自动暂停，其他不自动暂停，可重写
    open var autoPause: Bool {
        switch self.category {
        case .block: return true
        default: return false
        }
    }
    /// 播放器是否被当前层暂停中
    open var isAutoPaused: Bool {
        if let player = self.playerManager, let flag = self.autoPauseFlag {
            return player.isPaused(for: flag)
        }
        return false
    }
    /// 点击空白处自动取消激活
    open var isDeactiveForTapBlank: Bool {
        switch self.category {
        case .control: return true
        default: return false
        }
    }
    
    open var containerView: UIView? {
        didSet {
            if oldValue !== self.containerView {
                if let old = oldValue {
                    self.unloadContainerView(old)
                }
                if let new = self.containerView {
                    self.loadContainerView(new)
                }
            }
        }
    }
    
    public override init() {
        super.init()
    }
    
    open func show(completion:@escaping () -> Void) {
        if let containerView = containerView {
            self.aniamteShow(for: containerView, completion: completion)
        }else{
            completion()
        }
    }
    
    open func hide(completion:@escaping () -> Void) {
        if let containerView = containerView {
            self.aniamteHide(for: containerView, completion: completion)
        }else{
            completion()
        }
    }
    
    /// 用于临时提高弹窗等级
    open func active(at level:VideoPlayerLayerLevel) {
        self.tempLevel = level
        self.active()
    }
    
    open func willActive() {
        
    }
    open func willDeactive() {
        self.tempLevel = nil
    }
    
    open func didActived() {
        
    }
    open func didDeactived() {
        
    }
    /// 设置player
    open func loadPlayerManager(_ player: VideoPlayerManagerType) {
        
    }
    open func unloadPlayerManager(_ player: VideoPlayerManagerType) {
        
    }
    /// 设置manager
    open func loadManager(_ manager: VideoPlayerLayerManagerType) {
        
    }
    open func unloadManager(_ manager: VideoPlayerLayerManagerType) {
        
    }
    /// 设置containerView
    open func loadContainerView(_ containerView: UIView) {
        
    }
    open func unloadContainerView(_ containerView: UIView) {
        
    }
    
    deinit {
        self.playerManager = nil
        self.manager = nil
    }
}
extension VideoPlayerBaseLayer {
    /// 获取进度时间字符串
    open func timeString(_ time: TimeInterval) -> String{
        let minute = Int(time / 60)
        let sec = Int(time) - minute * 60
        var minuteStr = minute.description
        var secStr = sec.description
        if minute < 10 {
            minuteStr = "0" + minuteStr
        }
        if sec < 10 {
            secStr = "0" + secStr
        }
        return minuteStr + ":" + secStr
    }
}
